<?php
$basedir = dirname(__FILE__);

require_once $basedir . '/../classes/minime.php';
require_once $basedir . '/../extlib/Crypt/RSA.php';
require_once $basedir . '/../extlib/Crypt/Random.php';
require_once $basedir . '/../extlib/Crypt/Hash.php';
require_once $basedir . '/../extlib/Math/BigInteger.php';

MiniMe::initialize();

if( count($argv) < 4) {
      echo "\n\tERROR!";
      echo "\n\t\tPlease provide USERNAME URI MESSAGE as parameters to this function.";
      echo "\n\n\tExample:";
      echo "\n\t\t" . 'php blogto.php koski "http://identi.ca/group/ostatus" "Ready to Rock\'n\'Roll! "';
      echo "\n\n";
      exit;
}

$username = $argv[1];
$groupURI = $argv[2];
$msg      = $argv[3];

if(isset($argv[4]) && isset($argv[5])) {
    $ref = $argv[4];
    $href = $argv[5];    
} else {
    $ref = false;
    $href = false;
}


$userLoader = new Minime_User_Loader(Minime::$conf);
$user = $userLoader->getUser($username);

if(!$user) {
    echo "\n\t\tDude, the username you want to send the message from does not exists!";
    echo "\n\n";
    exit;
}

// Let's discover the dude we want to reply.

$discoverer = new Discovery_Uri($groupURI);
$discoverer->__debugoutput = true;
if(!$discoverer->discover()) {
     echo "\n\tDiscovery of '" . $groupURI . "' Failed!\n\n";
     exit;
}

echo "\n\t\t- Name   : '" . $discoverer->getName() . "'.";
echo "\n\t\t- Hub    : '" . $discoverer->getHubUri() . "'.";
echo "\n\t\t- Salmon : '" . $discoverer->getSalmonUri() . "'.";

$sal = new Salmon_Message();
if(!$sal->createAtomEntryAndSendDoEverythingThisIsTemporarTest($user, array($discoverer->getFeedsAuthorUri()), $msg, $discoverer->getSalmonUri(), $ref, $href)) {
    echo "\n\tSalmon post FAILED! (Sorry, log does not yet say why, bad cod here :-( )\n\n";    
    exit;
} else {
    echo "\n\t\t- Salmon notice OK.";    
}

echo "\n\n\tDONE!\n";
